﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace BTv2.Model
{
    public class User : INotifyPropertyChanged
    {


        public event PropertyChangedEventHandler PropertyChanged;

        // This method is called by the Set accessor of each property. 
        // The CallerMemberName attribute that is applied to the optional propertyName 
        // parameter causes the property name of the caller to be substituted as an argument. 
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private string _mail;

        public User(int id, string username,string password, string mail, bool AdminStatus)
        {
            ID = id;
            UserName = username;
            _password = password;
            Mail = mail;
            IsAdmin = AdminStatus;
        }

        public User()
        {

        }

       private string _password;

       public int ID
       {
           get;
           set;
       }
       
       public string UserName
       {
           get;
           set;
       }

       public string Password
       {
           get
           {
               return _password;
           }
           set
           {
               _password = GetMD5Hash(value);
           }
       }

       public string Mail
       {
           get
           {
               return _mail;
           }
           set
           {
               if (value != _mail)
               {
                   _mail = value;
                   NotifyPropertyChanged();
               }
           }
       }

       public bool IsAdmin
       {
           get;
           set;
       }

       /// <summary>
       /// Returns a MD5 hash as a string
       /// </summary>
       /// <param name="TextToHash">String to be hashed.</param>
       /// <returns>Hash as string.</returns>
       public static  string GetMD5Hash(String TextToHash)
       {
           //Check wether data was passed
           if ((TextToHash == null) || (TextToHash.Length == 0))
           {
               return String.Empty;
           }

           //Calculate MD5 hash. This requires that the string is splitted into a byte[].
           MD5 md5 = new MD5CryptoServiceProvider();
           byte[] textToHash = Encoding.Default.GetBytes(TextToHash);
           byte[] salt = Encoding.Default.GetBytes(Properties.Settings.Default.salt);
           byte[] result = md5.ComputeHash(textToHash);
           result = md5.ComputeHash(result.Concat(salt).ToArray());
           //Convert result back to string.
           return System.BitConverter.ToString(result).Replace("-","").ToLower();
       }
    }
}
